package com.example.springboot.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Book;
import com.example.springboot.mapper.BookMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RequestMapping("/book")
@RestController
public class BookController {
    @Resource
    BookMapper bookMapper;

    @PostMapping("/get")
    public Result<?> get(){
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        List<Book> list =  bookMapper.selectList(wrapper);
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result<?> upload(MultipartFile file) throws IOException {
        String originalFileName = file.getOriginalFilename();
        String rootFilePath = System.getProperty("user.dir") + "/springboot/src/main/resources/files/" + originalFileName;
        FileUtil.writeBytes(file.getBytes(),rootFilePath);
        return Result.success();
    }

}
